package com.rbw.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.rb.util.StringUtil;
import com.rbw.model.IndustryBean;
import com.rbw.model.OriginBean;
import com.rbw.model.TechBean;
import com.rbw.model.TechCategoryBean;
import com.rbw.model.TradeTypeBean;
import com.rbw.model.UserBean;
import com.rbw.service.TechService;
import com.rbw.service.UserService;

@Controller
public class PageController {

	
	private Logger logger = Logger.getLogger(this.getClass());
	
    @Autowired
    TechService techServiceImpl;

	@Autowired
	UserService userServiceImpl;

	@RequestMapping("/index")
	public ModelAndView toIndex() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("index");

		return mv;
	}

	@RequestMapping("/user")
	public ModelAndView toUser() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("user");

		return mv;
	}

	@RequestMapping("/login")
	public ModelAndView toLogin(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		String sessionId = request.getSession().getId();
		logger.info("sessionId:" + sessionId);
		UserBean user = userServiceImpl.existUser(sessionId);
		if(user!=null) {
			mv.setViewName("user");
		} else{
			mv.setViewName("login");
		}
		return mv;
	}
	
	@RequestMapping("/tech/list")
	public ModelAndView toTechList() {
		ModelAndView mv = new ModelAndView();
		List<TechCategoryBean> categories = techServiceImpl.getCategories();

		List<IndustryBean> industries = techServiceImpl.getIndustries();

		List<OriginBean> origins = techServiceImpl.getOrigins();
		List<TradeTypeBean> types = techServiceImpl.getTradeType();
		
		mv.addObject("categories", categories);
		mv.addObject("industries", industries);
		mv.addObject("origins", origins);
		mv.addObject("types", types);
		
		mv.setViewName("tech_list");
		return mv;
	}
	
	@RequestMapping("/tech/detail")
	public ModelAndView toTechDetail(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		TechBean bean;
		String techId = StringUtil.toString(request.getParameter("techId"), null);
        if (StringUtil.checkEmpty(techId)) {
            bean = new TechBean();
        } else {
            bean = techServiceImpl.getDetail(techId);
            String rTime = bean.getReleaseTime() != null 
            		? sdf.format(bean.getReleaseTime()) : sdf.format(new Date());
            bean.setrTime(rTime);
        }

        mv.addObject("tech", bean);

		mv.setViewName("tech_detail");
		return mv;
	}
	
	@RequestMapping("/made")
	public ModelAndView toMade(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();

		mv.setViewName("made");
		return mv;
	}
	
	@RequestMapping("/toHomeEnter")
	public ModelAndView toHomeEnter(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();

		mv.setViewName("home_enter");
		return mv;
	}
	
	@RequestMapping("/toMadeEnter")
	public ModelAndView toMadeEnter(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();

		mv.setViewName("made_enter");
		return mv;
	}
	
	@RequestMapping("/toInnovate")
	public ModelAndView toInnovate(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();

		mv.setViewName("innovate");
		return mv;
	}
	
	@RequestMapping("/toOrderMain")
	public ModelAndView toOrderMain(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();

		mv.setViewName("order/main");
		return mv;
	}
	
	@RequestMapping("/toOrderPCB")
	public ModelAndView toOrderPCB(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();

		mv.setViewName("order/order_PCB");
		return mv;
	}
	
	@RequestMapping("/toOrderLayout")
	public ModelAndView toOrderLayout(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();

		mv.setViewName("order/order_layout");
		return mv;
	}
	@RequestMapping("/toOrderSMT")
	public ModelAndView toOrderSMT(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();

		mv.setViewName("order/order_SMT");
		return mv;
	}
	
	@RequestMapping("/toOrderIron")
	public ModelAndView toOrderIron(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();

		mv.setViewName("order/order_Iron");
		return mv;
	}
	

}
